package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class FileUtils {
	/**
	 * get the original content of a text file without parsing it.
	 * @param fileName
	 * @return
	 */
	public static String getFileContent(String fileName) {
		BufferedReader br;
		try {
			br = new BufferedReader(
					new InputStreamReader(new FileInputStream(fileName), "utf-8"));
			StringBuilder sb = new StringBuilder();
			String line;
			while ((line = br.readLine()) != null) {
				sb.append(line);
				sb.append('\n');
			}
			br.close();
			return sb.toString();
		} catch (FileNotFoundException e) {
			System.err.println("File " + fileName + " not found!");
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("Error opening file " + fileName);
			e.printStackTrace();
		}
		return "";
	}
	
	public static void writeStringToFile(String fileName, String content) throws IOException {
		File file = new File(fileName);
		file.createNewFile();
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
		osw.append(content);
		osw.close();
	}
	
}
